package com.fc.test.controller;

import com.fc.test.common.PageUtils;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.service.ForeSchoolNoticeService;
import com.fc.test.service.ForeSchoolService;
import com.fc.test.service.ForeUserService;
import com.fc.test.vo.ForeClassVo;
import com.fc.test.vo.ForeSchoolVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class MySelfController  extends BaseController {
    @Autowired
    ForeSchoolNoticeService foreSchoolNoticeService;
    @Autowired
    private ForeSchoolService foreSchoolService;
    @Autowired
    private ForeUserService foreUserService;
    @Autowired
    ForeClassMapper foreClassMapper;
    @Autowired
    ForeClassMessageMapper foreClassMessageMapper;
    @Autowired
    ForeClassUserMapper foreClassUserMapper;
    @Autowired
    ForeFileMapper foreFileMapper;


    @GetMapping("/ForeSchoolController/findAll")
    public Object findAll(){
        PageInfo<ForeSchool> page=foreSchoolService.list(PageUtils.getTablePar1000(),null) ;
        TableSplitResult<ForeSchoolVo> result=new TableSplitResult<ForeSchoolVo>(page.getPageNum(), page.getTotal(), getNotice(page.getList()));
        return  result;
    }

    @GetMapping("/ForeUserController/findByUsernameAndPassword")
    public ForeUser findByUsernameAndPassword(String username, String password){
        ForeUserExample example=new ForeUserExample();
        try {
            example.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
            List<ForeUser> list =foreUserService.selectByExample(example);
            return list.size()>0?list.get(0):null;
        }catch (Exception e){
            return null;
        }

    }

    @PostMapping("/ForeUserController/addUser")
    public AjaxResult addUser(@RequestBody ForeUser foreUser){
        int a=checkNameUnique(foreUser);
        if(a>0){
            return error("该用户已被注册");
        }
        int b=foreUserService.insertSelective(foreUser);
        if(b>0){
            return success();
        }else{
            return error();
        }
    }

    @RequestMapping("/ForeClass/myClasses")
    public Object myClasses(String userId){
        ForeClassUserExample foreClassUserExample=new ForeClassUserExample();
        foreClassUserExample.or().andUserIdEqualTo(userId);
        List<ForeClassUser> foreClassUserList=foreClassUserMapper.selectByExample(foreClassUserExample);
        List<String> classIds=new ArrayList();
        for(ForeClassUser foreClassUser:foreClassUserList){
            classIds.add(foreClassUser.getClassId());
        }

        ForeClassExample foreClassExample=new ForeClassExample();
        foreClassExample.or().andIdIn(classIds);
        List<ForeClass> foreClassList=foreClassMapper.selectByExample(foreClassExample);
        List<ForeClassVo> foreClassVoList=new ArrayList();
        for(ForeClass foreClass:foreClassList){
            ForeClassVo foreClassVo=new ForeClassVo();
            BeanUtils.copyProperties(foreClass,foreClassVo);
            ForeClassMessageExample foreClassMessageExample=new ForeClassMessageExample();
            foreClassMessageExample.or().andClassIdEqualTo(foreClass.getId());
            foreClassMessageExample.setOrderByClause("create_time DESC");
            List<ForeClassMessage> messages=foreClassMessageMapper.selectByExample(foreClassMessageExample);
            Integer count=0;
            for(ForeClassMessage message:messages){
                ForeUserExample example=new ForeUserExample();
                example.or().andIdEqualTo(message.getUserId()==null?"教师":message.getUserId());
                List<ForeUser> list=foreUserMapper.selectByExample(example);
                if(list.size()!=0){
                    message.setUsername(list.get(0).getName());
                }
                if(message.getIsread()==0){
                    count++;
                }
            }
            foreClassVo.setCount(count);
            foreClassVo.setForeClassMessages(messages);
            foreClassVoList.add(foreClassVo);
        }
         return foreClassVoList;
    }


    @RequestMapping("/ForeClass/findClass")
    public Object findClass(String name){
        ForeClassExample example=new ForeClassExample();
        if(name!=null&&!"".equals(name)){
            example.or().andNameLike("%"+name+"%");
        }
        return foreClassMapper.selectByExample(example);
    }
    @RequestMapping("/ForeClass/addClass")
    public Object addClass(ForeClassUser foreClassUser){

       ForeClassUserExample example=new ForeClassUserExample();
       example.or().andUserIdEqualTo(foreClassUser.getUserId()).andClassIdEqualTo(foreClassUser.getClassId());
       List list=foreClassUserMapper.selectByExample(example);
       if(list.size()!=0)return 0;
       foreClassUser.setId(UUID.randomUUID().toString());
       return foreClassUserMapper.insert(foreClassUser);
    }


    @RequestMapping("/ForeFile/fileList")
    public Object fileList(String name){
        if(name==null)name="";
        ForeFileExample example=new ForeFileExample();
        example.or().andNameLike("%"+name+"%");
        return foreFileMapper.selectByExample(example);
    }

    @Autowired
    ForeSchoolMapper schoolMapper;

    @RequestMapping("/ForeSchool/schoolList")
    public Object schoolList(){
        return schoolMapper.selectByExample(null);
    }
    @RequestMapping("/ForeClass/classList")
    public Object classList(){
        return foreClassMapper.selectByExample(null);
    }

    @Autowired
    public ForeUserMapper foreUserMapper;
    @RequestMapping("/ForeUser/userList")
    public Object userList(){
        return foreUserMapper.selectByExample(null);
    }
    @Autowired
    ForeTicketMapper foreTicketMapper;

    @RequestMapping("/ForeTicket/findByType")
    public Object findByType(String type,String userId){
        ForeTicketExample example=new ForeTicketExample();
        example.or().andTypeEqualTo(type).andUserIdEqualTo(userId);
        return foreTicketMapper.selectByExample(example);
    }

     @RequestMapping("/ForeUser/changeInfo")
     public Object changeInfo(@RequestBody ForeUser foreUser){
        return foreUserMapper.updateByPrimaryKeySelective(foreUser);
     }

     @RequestMapping("/ForeClassMsg/changeRead")
     public Object changeRead(String id){
        ForeClassMessage foreClassMessage=foreClassMessageMapper.selectByPrimaryKey(id);
        foreClassMessage.setIsread(1);
        return foreClassMessageMapper.updateByPrimaryKey(foreClassMessage);
     }

     @RequestMapping("/ForeClassMsg/sendMsg")
     public Object sendMsg(@RequestBody  ForeClassMessage foreClassMessage){
           foreClassMessage.setId(UUID.randomUUID().toString());
           return  foreClassMessageMapper.insertSelective(foreClassMessage);
     }








    private int checkNameUnique(ForeUser foreUser){
        int b=foreUserService.checkNameUnique(foreUser);
        if(b>0){
            return 1;
        }else{
            return 0;
        }
    }
    private List<ForeSchoolVo> getNotice(List<ForeSchool> list) {
        List<ForeSchoolVo> vos=new ArrayList<>();
        for(ForeSchool foreSchool:list){
            ForeSchoolNoticeExample example=new ForeSchoolNoticeExample();
            example.or().andSchoolIdEqualTo(foreSchool.getId());
            List<ForeSchoolNotice> notices=foreSchoolNoticeService.selectByExample(example);
            ForeSchoolVo vo=new ForeSchoolVo();
            BeanUtils.copyProperties(foreSchool,vo);
            vo.setSchoolNotices(notices);
            vos.add(vo);
        }
        return vos;
    }


}
